﻿using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AJWPFAdmin.Core.Crypto.AES
{
    public class AJAES
    {
        private static readonly byte[] _iv = new byte[16]
        {
            65, 114, 101, 121, 111, 117, 109, 121, 83, 110,
            111, 119, 109, 97, 110, 63
        };

        public static string EncryptCBC(string content, string encryptKey)
        {
            using Aes aes = Aes.Create();
            aes.Key = Encoding.UTF8.GetBytes(encryptKey);
            aes.IV = _iv;
            aes.FeedbackSize = 128;
            using ICryptoTransform cryptoTransform = aes.CreateEncryptor();
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            return Convert.ToBase64String(cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string EncryptWithBase64KeyECB(string content, string base64Key)
        {
            using Aes aes = Aes.Create();
            aes.Key = Convert.FromBase64String(base64Key);
            //aes.IV = _iv;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.FeedbackSize = 128;
            using ICryptoTransform cryptoTransform = aes.CreateEncryptor();
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            return Convert.ToBase64String(cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static byte[] EncryptWithBase64KeyECB(byte[] content, string base64Key)
        {
            using Aes aes = Aes.Create();
            aes.Key = Convert.FromBase64String(base64Key);
            //aes.IV = _iv;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.FeedbackSize = 128;
            using ICryptoTransform cryptoTransform = aes.CreateEncryptor();
            return cryptoTransform.TransformFinalBlock(content, 0, content.Length);
        }

        public static string DecryptCBC(string content, string encryptKey)
        {
            var encryptBytes = Convert.FromBase64String(content);
            using var aes = Aes.Create();
            aes.Key = Encoding.UTF8.GetBytes(encryptKey);
            aes.IV = _iv;
            aes.FeedbackSize = 128;
            var transform = aes.CreateDecryptor();
            return Encoding.UTF8.GetString(transform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length));
        }

        public static string DecryptWithBase64KeyECB(string content, string base64Key)
        {
            var encryptBytes = Convert.FromBase64String(content);
            using var aes = Aes.Create();
            aes.Key = Convert.FromBase64String(base64Key);
            //aes.IV = _iv;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.FeedbackSize = 128;
            var transform = aes.CreateDecryptor();
            return Encoding.UTF8.GetString(transform.TransformFinalBlock(encryptBytes, 0, encryptBytes.Length));
        }
    }
}
